package com.javasm.sys.controller;

import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.sys.RoleService;
import com.javasm.sys.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("roles")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;

    @GetMapping("all")
    public AxiosResult getAllRoles() {
        List<SysRole> allRoles = roleService.getAllRoles();
        return AxiosResult.suc(allRoles);
    }

    @GetMapping
    public AxiosResult getRoles(SysRole role) {
        List<SysRole> list = roleService.getRoles(role);
        return AxiosResult.suc(list);
    }
    @GetMapping("{roleId}")
    public AxiosResult getMenuId(@PathVariable String roleId) {
        List<Integer> menuIdList = roleService.getMenuId(roleId);
        return AxiosResult.suc(menuIdList);
    }
    @PostMapping
    public AxiosResult insertRoles(@RequestBody SysRole role) {
        Integer insert = roleService.insertRoleAndAuth(role);
        return suc(insert == 1);
    }
    //    修改用户
    @PutMapping
    public AxiosResult updateRole(@RequestBody SysRole role) {
//        System.out.println(role);
        Integer flag = roleService.updateRoleById(role);
        return isok(flag == 1);
    }

    @DeleteMapping("{ids}")
    public AxiosResult delUserById(@PathVariable  List<String> ids) {
        Integer flag = roleService.delRoleByIds(ids);
        return isok(flag == ids.size());
    }
}